﻿package com.cilico.single.components;

import com.cilico.single.ConfigManager;
import com.cilico.single.Mid;
import com.cilico.single.view.BaseView;
import com.cilico.single.view.DisplayPoi;

/**
 * Basic list view for all POIs from channels other than calendar.
 */
public class PoiListSelection extends PoiSelection {
	BaseView activity_;

	public PoiListSelection(Mid mid) {
		super(mid);
	}

	/**
	 * Get the first N POIs, display them and insert them into our selection set.
	 */
	public void populate(PoiRepository r) {
		super.populate(r);
		// NOTE: we need to create a new List. Because we set Font on list
		// elements,
		// if we don't do this, Sun's MIDP 2.0 reference implementation will
		// throw an
		// exception as we dynamically add new elements to the list.
		removeAllElements();
		// add new poi as the first one.
		POI newPoi = r.newPoi();
		if (newPoi != null && newPoi.name() != null) {
			// Display new POI always as number 0
			newPoi.name_ = newPoi.name().trim();
			DisplayPoi dp = mid_.getCurrentPoiSelection(false, activity_).addPOI(newPoi);
			dp.setDisplayIndex(0);
		}
		// add other pois.
		for (int i = 0; i < r.size() && i < ConfigManager.POIS_PER_SCREEN; ++i) {
			Object pp = r.getPOI(i);
			POI p;
			if (pp instanceof POI) {
				p = (POI) pp;
				if (p.name() != null)
					p.name_ = p.name().trim();
			} else {
				continue;
			}
			boolean isTempSel = false;
			/*
			 * if (r == mid_.lastValidPoiRepo_) isTempSel = true;
			 */
			DisplayPoi dp = mid_.getCurrentPoiSelection(isTempSel, activity_).addPOI(p);
			int idx = i + 1;
			dp.setDisplayIndex(idx);
		}
		// populate the view.
		// bv_.populateImpl();
	}

}
